package com.sc.dao.impl;

import com.sc.dao.ProductDao;
import com.sc.entity.Product;
import com.sc.utils.JDBCUtils;

import java.util.List;

public class ProductDaoImpl implements ProductDao {
    @Override
    public int add(Product product) throws Exception {
        String sql= "insert into product(id,name,category_id,price,amount)values(?,?,?,?,?)";
        return JDBCUtils.update(sql,new Object[]{product.getId(),product.getName(),product.getCategoryId(),
               product.getPrice() ,product.getAmount()});

    }

    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from product where id = ?";
        return JDBCUtils.update(sql,new Object[]{id});
    }

    @Override
    public int update(Product product) throws Exception {
        String sql = "update product set name = ?, category_id = ? , price =?" +
                "amount = ?";
        return JDBCUtils.update(sql,new Object[]{product.getName(),product.getCategoryId(),
                product.getPrice() ,product.getAmount()});
    }

    @Override
    public Product query(Integer productId) throws Exception {
        String sql="select from product where id = ?";
        return JDBCUtils.query(sql,Product.class,new Object[productId]);
    }

    @Override
    public Product queryById(int id) throws Exception {
        String sql = "select from product where id = ?";
        return JDBCUtils.query(sql,Product.class,new Object[id]);
    }



    @Override
    public List<Product> queryAll() throws Exception {
        String sql = "select from product ";
        return JDBCUtils.querylist(sql,Product.class,new Object[0]);
    }
}
